package com.cruise.game.common.api;

/**
 * API自定义异常
 *
 * @author cruise
 * @date 2025-02-28
 */
public class ApiException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private int code;

    /**
     * 错误消息
     */
    private String message;

    public ApiException(String message) {
        super(message);
        this.code = ApiCode.BUSINESS_ERROR;
        this.message = message;
    }

    public ApiException(int code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    /**
     * 抛出业务异常
     *
     * @param message 消息
     */
    public static void throwBusinessException(String message) {
        throw new ApiException(ApiCode.BUSINESS_ERROR, message);
    }

    /**
     * 抛出业务异常
     *
     * @param code    错误码
     * @param message 消息
     */
    public static void throwBusinessException(int code, String message) {
        throw new ApiException(code, message);
    }

    /**
     * 抛出参数错误异常
     *
     * @param message 消息
     */
    public static void throwParamException(String message) {
        throw new ApiException(ApiCode.PARAM_ERROR, message);
    }

    /**
     * 抛出未授权异常
     *
     * @param message 消息
     */
    public static void throwForbiddenException(String message) {
        throw new ApiException(ApiCode.FORBIDDEN, message);
    }

    /**
     * 抛出资源不存在异常
     *
     * @param message 消息
     */
    public static void throwNotFoundException(String message) {
        throw new ApiException(ApiCode.NOT_FOUND, message);
    }

    /**
     * 抛出系统异常
     *
     * @param message 消息
     */
    public static void throwSystemException(String message) {
        throw new ApiException(ApiCode.SYSTEM_ERROR, message);
    }
}
